package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.domain.Order;
import com.ssm.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @RequestMapping("/find_All.do")
    /**
     * @return : org.springframework.web.servlet.ModelAndView
     * @Description : 获取所有的角色
     * @param : []
     */
    public ModelAndView find_All() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Order> ordersList = orderService.findAllOrders();
        mv.addObject("ordersList", ordersList);
        mv.setViewName("order-list");
        return mv;
    }

    @RequestMapping("/findAll.do")
    /**
     * @return : org.springframework.web.servlet.ModelAndView
     * @Description : 获取所有角色，并通过PageHelper.startPage(page,size)实现分页操作
     * @param : [page, size], page：页码, size：分页长度
     */
    public ModelAndView findAll(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "4") Integer size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Order> ordersList = orderService.findAll(page, size);
        //PageInfo就是一个分页的bean
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("order-page-list");
        return mv;
    }

    @RequestMapping("/findById.do")
    /**
     * @return : org.springframework.web.servlet.ModelAndView
     * @Description : 通过id获取角色的详细信息
     * @param : [id]
     */
    public ModelAndView findById(@RequestParam(name = "id") String id) throws Exception {
        ModelAndView mv = new ModelAndView();
        Order orders = orderService.findById(id);
        mv.addObject("orders", orders);
        mv.setViewName("orders-show");
        return mv;
    }

}
